import json
from multiprocessing import Queue#全局，用于多进程，并行化
from handle_mongo import mongo_info
from concurrent.futures import ThreadPoolExecutor
#创建队列
queue_list=Queue()
list_theme=[]
import requests
index_major = 1
index_menu=1
def handle_request(url,data):#替换正则(.*?):(.*?)$    "$1":"$2",
    header={
        #"Cookie": "duid=70891494",
        "client": "4",
        "version": "7200.4",
        "channel": "oppo",
        "pset": "0",
        "terms-accepted": "1",
        "newbie": "1",
        #"pseudo-id": "174496425466244",
        "device": "PDRM00",
        "brand": "OPPO",
        "sdk": "30,11",
        "resolution": "2400*1080",
        "dpi": "3.0",
        "timezone": "28800",
        "language": "zh",
        "uuid": "88797e4e-3e3c-4cf0-8433-303aeee72a8e",
        "User-Agent": "Mozilla/5.0(Linux;Android11;PDRM00Build/RKQ1.200903.002;wv)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/83.0.4103.106MobileSafari/537.36",
        "battery-level": "0.68",
        "battery-state": "2",
        "caid": "174496425466244",
        "bssid": "02:00:00:00:00:00",
        "display-resolution": "2293*1080",
        "scale": "3.0",
        "reach": "1",
        "syscmp-time": "1638462056000",
        "countrycode": "CN",
        "sysmemory": "12071137280",
        "sysdisksize": "242GB",
        "bootmark": "e3300680-469c-453a-b577-4d68aecf91e1",
        "updatemark": "1672829271.416964460",
        "is-hmos": "false",
        "app-state": "0",
        "oaid": "B9EBDE1C3C004B578CF4487D3B01C8C56a0bd013307189ba72f162a20a62bfe6",
        "aaid": "DB503C0BC01347CB8E4589CBE77283936deea7b1513feaaed7bf04f6a220db7b",
        "act-code": "1672829276",
        "act-timestamp": "1672829276",
        "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
        "Accept-Encoding": "gzip,deflate",
        "Connection": "Keep-Alive",
        "session-info": "O0y/dPYTJAm0BbGoew14b1zhl1gXFMOZQiTAeT7JFuBOp/DD/k6TI003eoCKgMRjaTJMf1tFgVGnxvNTVr09JQ==",
        "Host": "api.douguo.net"
        #"Content-Length": "114"
    }
    response=requests.post(url=url,headers=header,data=data)
    return response
def hanle_index():
    url="https://api.douguo.net/recipe/flatcatalogs"
    data={
        "client": "4",
        #"_session": "1672886887840v=1503650468",
        "_vs": "0/nsign_ran=5da89c644da4401180288a365d6f31f5",
        "code": "b196a0c0b2651259"
    }
    response=handle_request(url=url,data=data)
    # print(response.text)
    index_response_dict=json.loads(response.text)
    for index_item in index_response_dict['result']['cs']:#为了找到一个执行地毯式解析，先泛式后举例
        # print(index_item)
            for index_item_1 in index_item['cs']:
                # print(index_item_1)
                for item in index_item_1['cs']:
                    data_2={
                        "client": "4",
                        #"_session": "1672886887840",
                        "keyword": item['name'],
                        "order": "0",
                        "_vs": "11111",
                        "type": "0",
                        "auto_play_mode": "2",
                        "sign_ran": "8d5801626103349790cef7d2511e503d",
                        "code": "b2d6b396b177ca1e"
                    }
                    # print(data_2)
                    queue_list.put(data_2)
                    list_theme.append(item['name'])#装入列表
def handle_caipu_list(data):
    print("当前处理的食材",data['keyword'],'_',index_major)
    ++index_major
    caipu_list_url='https://api.douguo.net/recipe/v2/search/0/20'
    caipu_list_response=handle_request(url=caipu_list_url,data=data)
    # print(caipu_list_response.text)
    caipu_list_response_dict=json.loads(caipu_list_response.text)
    for item in caipu_list_response_dict['result']['list']:
        # print(item)
        caipu_info={}
        caipu_info['shicai']=data['keyword']
        if item['type']==13:
            caipu_info['cookstory']=item['r']['cookstory'].replace('\n','').replace(' ','').replace('\r','')
            caipu_info['tips'] = item['r']['tips']
            caipu_info['img'] = item['r']['img']
            caipu_info['cook_difficulty']=item['r']['cook_difficulty']
            caipu_info['cook_time']=item['r']['cook_time']
            caipu_info['major']=item['r']['major']
            caipu_info['cookname']=item['r']['n']
            caipu_info['id']=item['r']['id']
            detail_url='https://api.douguo.net/recipe/v2/detail/'+str(caipu_info['id'])
            # print(detail_url)
            response_caipu_step=handle_request(url=detail_url,data='')
            json_caipu_step=json.loads(response_caipu_step.text)
            # print(json_caipu_step['result']['recipe']['cookstep'])
            caipu_info['cook_step']=json_caipu_step['result']['recipe']['cookstep']
            # print(json.dumps(caipu_info))#json.dumps将Python数据结构转换为JSON
            print('当前入库的菜谱：',caipu_info['cookname'],'_',index_major-1,'_',index_menu)
            ++index_menu
            case_caipu_info=mongo_info
            case_caipu_info.insert_item(caipu_info)
        else:
            continue
hanle_index()
print(list_theme)
# pool=ThreadPoolExecutor(max_workers=2)
# while queue_list.qsize()>0:
#     print("队列大小：",queue_list.qsize())
#     pool.submit(handle_caipu_list,queue_list.get())

count=queue_list.qsize()
for i in range(count):
    print("序号_规则排序_",i)
    handle_caipu_list(queue_list.get())

# handle_caipu_list(queue_list.get())
# print(queue_list.qsize())